package com.zhuss.idea.idea;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.idea.idea.DetailIdeaBo;
import com.zhuss.bean.bo.idea.idea.SearchIdeaBo;
import com.zhuss.bean.bo.idea.idea.wechat.ListIdeaBo;
import com.zhuss.bean.bo.core.upload.DownloadBo;
import com.zhuss.bean.vo.idea.idea.DetailIdeaVo;
import com.zhuss.bean.vo.idea.idea.ListIdeaVo;
import com.zhuss.bean.vo.core.upload.DownloadVo;
import com.zhuss.idea.processor.IdeaAppendRead;
import com.zhuss.idea.processor.IdeaRead;
import com.zhuss.idea.processor.IdeaTopicRead;
import com.zhuss.common.PageVo;
import com.zhuss.common.Processor;
import com.zhuss.common.ReadService;
import com.zhuss.enums.AppendTypeEnum;
import com.zhuss.exception.WpsException;


/**
 * idea查询service（基本查询）
 * @author zhushanshuo
 * @Date 2020-02-05 15:59
 *
 */
@Component("ideaReadService")
public class IdeaReadService implements ReadService<Object, Object> {
	
	@Autowired
	@Qualifier("ideaRead")
	private IdeaRead ideaRead;

	@Autowired
	@Qualifier("ideaTopicRead")
	private IdeaTopicRead ideaTopicRead;
	
	@Autowired
	@Qualifier("ideaAppendRead")
	private IdeaAppendRead ideaAppendRead;
	
	@Autowired
	@Qualifier("downloadProcessor")
	private Processor<DownloadVo, DownloadBo> downloadProcessor;
	
	
	
	/**
	 * 详情
	 */
	@Override
	public Object detail(Object param) throws WpsException {
		DetailIdeaBo bo = (DetailIdeaBo) param;
		Long ideaId = bo.getIdeaId();
		DetailIdeaVo vo = (DetailIdeaVo) ideaRead.detail(ideaId);
		DownloadBo downloadBo = new DownloadBo();
		downloadBo.setKeyList(vo.getPic());
		DownloadVo downloadVo = downloadProcessor.process(downloadBo);
		vo.setUrlList(downloadVo.getUrlList());
		return vo;
	}
	
	/**
	 * 列表
	 * @throws WpsException 
	 */
	@Override
	public Object list(Object param) throws WpsException {
		SearchIdeaBo bo = (SearchIdeaBo) param;
		Object result = ideaRead.list(bo);
		transfer(result, bo.getConcurrentUserId());
		return result;
	}
	
	/**
	 * 树
	 * @throws WpsException 
	 */
	public Object listTopicTree(DetailIdeaBo param) throws WpsException {
		Long ideaId = param.getIdeaId();
		Object result = ideaTopicRead.listTopicTree(ideaId);
		return result;
	}
	
	/**
	 * 列表
	 * @throws WpsException 
	 */
	public Object listIdea(Object param) throws WpsException {
		ListIdeaBo bo = (ListIdeaBo) param;
		Object result = ideaRead.listIdea(bo);
		transfer2(result, bo.getConcurrentUserId());
		return result;
	}
	
	@SuppressWarnings("unchecked")
	private void transfer2(Object result, Long userId) throws WpsException {
		PageVo<DetailIdeaVo> pageVo = (PageVo<DetailIdeaVo>) result;
		for (DetailIdeaVo vo : pageVo.getItems()) {
			DownloadBo downloadBo = new DownloadBo();
			downloadBo.setKeyList(vo.getPic());
			DownloadVo downloadVo = downloadProcessor.process(downloadBo);
			vo.setUrlList(downloadVo.getUrlList());
			
			Integer thumbNum = ideaAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getIdeaId(), null).size();
			Integer collectNum = ideaAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getIdeaId(), null).size();
			Integer forwardNum = ideaAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getIdeaId(), null).size();
			Integer topicNum = ideaTopicRead.listTopic(vo.getIdeaId(), 0l).size();
			
			Integer thumbSelfNum = ideaAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getIdeaId(), userId).size();
			Integer collectSelfNum = ideaAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getIdeaId(), userId).size();
			Integer forwardSelfNum = ideaAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getIdeaId(), userId).size();
			
			vo.setThumbNum(thumbNum);
			vo.setCollectNum(collectNum);
			vo.setForwardNum(forwardNum);
			vo.setTopicNum(topicNum);
			
			vo.setThumbSelfNum(thumbSelfNum);
			vo.setCollectSelfNum(collectSelfNum);
			vo.setForwardSelfNum(forwardSelfNum);
		}
	}
	
	@SuppressWarnings("unchecked")
	private void transfer(Object result, Long userId) throws WpsException {
		PageVo<ListIdeaVo> pageVo = (PageVo<ListIdeaVo>) result;
		for (ListIdeaVo vo : pageVo.getItems()) {
			DownloadBo downloadBo = new DownloadBo();
			downloadBo.setKeyList(vo.getPic());
			DownloadVo downloadVo = downloadProcessor.process(downloadBo);
			vo.setUrlList(downloadVo.getUrlList());
			
			Integer thumbNum = ideaAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getIdeaId(), null).size();
			Integer collectNum = ideaAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getIdeaId(), null).size();
			Integer forwardNum = ideaAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getIdeaId(), null).size();
			Integer topicNum = ideaTopicRead.listTopic(vo.getIdeaId(), 0l).size();
			
			Integer thumbSelfNum = ideaAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getIdeaId(), userId).size();
			Integer collectSelfNum = ideaAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getIdeaId(), userId).size();
			Integer forwardSelfNum = ideaAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getIdeaId(), userId).size();
			
			vo.setThumbNum(thumbNum);
			vo.setCollectNum(collectNum);
			vo.setForwardNum(forwardNum);
			vo.setTopicNum(topicNum);
			
			vo.setThumbSelfNum(thumbSelfNum);
			vo.setCollectSelfNum(collectSelfNum);
			vo.setForwardSelfNum(forwardSelfNum);
			
		}
	}
	
	
}

